package com.project.nimsen.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.project.jdbc.web.BaseMVCAction;
import com.project.nimsen.common.web.LoginUser;
import com.project.nimsen.util.WebUtil;

@Controller
public class Login extends BaseMVCAction {
	@Resource
	WebUtil webUtil;
	@RequestMapping(value = "/login")
	public String login() {
		return "/login";
	}
	@RequestMapping(value = "/loginon")
	public String loginon(HttpServletRequest request) {
		Subject user = SecurityUtils.getSubject();
		String username = getParameter("username");
		String password = getParameter("password");
		//password = MD5Encrypt.encoderByMd5With32Bit(password);
		UsernamePasswordToken token = new UsernamePasswordToken(username,
			password);
		token.setRememberMe(true);
		try {
			user.login(token);
			LoginUser cur_user = LoginUser.getCurr();
			getRequest().setAttribute("rolename", cur_user.getRole_name());
			getRequest().setAttribute("truename", cur_user.getTruename());
		} catch (IncorrectCredentialsException e) {
			e.printStackTrace();
			getRequest().setAttribute("message", "用户名密码错误");
			logger.error(e);
			return "/login";
		} catch (Exception e) {
			e.printStackTrace();
			getRequest().setAttribute("message", "用户名密码错误");
			logger.error(e);
			return "/login";
		}
		return "/WEB-INF/sys/main";
	}
}